//
//  LKContinuInvestViewController.m
//  YLHelper
//
//  Created by she1323 on 2018/5/4.
//  Copyright © 2018年 she1323. All rights reserved.
//



#import "LKContinuInvestViewController.h"
#import "LKWriteContinuInvestCell.h"
#import "LKBottomButtonView.h"
#import "LKContinuInvestOrderListViewController.h"
#import "GFDatePicker.h"
#import "LKConfirmCustomInforViewController.h"
@interface LKContinuInvestViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)LKBaseTableview *tableview;
@property(nonatomic,strong)NSArray *titlesArray;
@property(nonatomic,strong)LKBottomButtonView *bottomView;
@property(nonatomic,assign)BOOL isHandInput;
@end

@implementation LKContinuInvestViewController
-(LKCustomerModel *)customerModel{
    if (!_customerModel) {
        _customerModel = [[LKCustomerModel alloc] init];
    }
    return _customerModel;
}
-(LKAddOrderModel *)addOrderModel{
    if (!_addOrderModel) {
        _addOrderModel = [[LKAddOrderModel alloc] init];
        _addOrderModel.IsContinuedInvestment = @"1";
    }
    return _addOrderModel;
}
-(LKBaseTableview *)tableview{
    if (!_tableview) {
        WS(weakSelf)
        _bottomView = [[[NSBundle mainBundle] loadNibNamed:@"LKBottomButtonView" owner:self options:nil] lastObject];
        [self.view addSubview:_bottomView];
        [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
            make.height.mas_equalTo(69);
        }];
        RAC(_bottomView.nextBtn,enabled) = [RACSignal combineLatest:@[RACObserve(weakSelf.addOrderModel, PreviousContractCode),RACObserve(weakSelf.addOrderModel, CalulationDate),RACObserve(weakSelf.addOrderModel, PreviousOrderInvestmentAmount)] reduce:^id _Nullable(NSString *preContractcode,NSString * caculatDate,NSString *money){
            return @(money != nil && caculatDate != nil && preContractcode != nil);
        }];
        [_bottomView.nextBtn setBackgroundImage:[UIImage imageWithColor:blueDefault] forState:UIControlStateNormal];
        [_bottomView.nextBtn setBackgroundImage:[UIImage imageWithColor:[UIColor lightGrayColor]] forState:UIControlStateDisabled];
        
        [[_bottomView.nextBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
//            if (!weakSelf.addOrderModel.PreviousContractCode) {
//                [SVProgressHUD showInfoWithStatus:@"请选择续投单合同"];
//                return ;
//            }
//            if (!weakSelf.addOrderModel.PreviousOrderInvestmentAmount) {
//                [SVProgressHUD showInfoWithStatus:@"请输入原合同金额"];
//                return ;
//            }
//            if (!weakSelf.addOrderModel.CalulationDate) {
//                [SVProgressHUD showInfoWithStatus:@"请选择原合同起息日期"];
//                return ;
//            }
            LKConfirmCustomInforViewController *vc = [[LKConfirmCustomInforViewController alloc] init];
            vc.addOrderModel = weakSelf.addOrderModel;
            vc.model = weakSelf.customerModel;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }];
        _tableview = [LKBaseTableview setTableviewWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT + 10, WIDTH , HEIGHT - NAVIGATION_BAR_HEIGHT - 10 - 70) style:UITableViewStylePlain registercellClasss:@[@"LKWriteContinuInvestCell"]];
        [self.view addSubview:_tableview];
        _tableview.delegate = self;
        _tableview.bounces = NO;
        _tableview.dataSource = self;
        _tableview.backgroundColor = [UIColor whiteColor];
        _tableview.tableFooterView = [[UIView alloc] init];
    }
    return _tableview;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.isHandInput = NO;
    self.tableview.estimatedRowHeight = 200;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getCusotmerInformation:) name:@"sendContinueOrderInfor" object:nil];
    // Do any additional setup after loading the view.
}
-(void)getCusotmerInformation:(NSNotification *)notice{
    NSDictionary *dic = notice.userInfo;
    NSString *customerCode = [[NSUserDefaults standardUserDefaults] objectForKey:@"fuckContinueOrderKey"];
    NSString *contractCode = [[NSUserDefaults standardUserDefaults] objectForKey:@"fuckContinueContractKey"];
    self.addOrderModel.PreviousContractCode = contractCode;
    
    self.customerModel.customerCode = customerCode;
    self.addOrderModel.CustomerCode = customerCode;
    self.isHandInput = YES;
    [self.tableview reloadData];
    NSLog(@"%@",dic);
}
-(void)setNavgationBackBtn{
    [super setNavgationBackBtn];
    self.navigationItem.title = @"填写续投单";
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titlesArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LKWriteContinuInvestCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKWriteContinuInvestCell"];
    cell.titleLabel.text = self.titlesArray[indexPath.row];
    if (self.isHandInput) {
        WS(weakSelf)
        if (indexPath.row != 1) {
            cell.arrowIcon.hidden = NO;
            cell.contentLabel.hidden = NO;
            cell.contentTF.hidden = YES;
            if (indexPath.row == 0) {
                cell.contentLabel.text = self.addOrderModel.PreviousContractCode;
                cell.arrowIcon.hidden = NO;
            }
            if (indexPath.row == 2) {
                cell.contentLabel.text = self.addOrderModel.CalulationDate;
            }
        }else{
            cell.arrowIcon.hidden = YES;
            cell.contentTF.hidden = NO;
            cell.contentTF.textAlignment = NSTextAlignmentRight;
            cell.contentLabel.hidden = YES;
            [cell.contentTF.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
                weakSelf.addOrderModel.PreviousOrderInvestmentAmount = x;
            }];
            cell.contentTF.text = self.addOrderModel.PreviousOrderInvestmentAmount;
        }
        return cell;
    }else{
        cell.arrowIcon.hidden = YES;
        cell.contentLabel.hidden = NO;
        cell.contentTF.hidden = YES;
        if (indexPath.row == 0) {
            cell.contentLabel.text = self.addOrderModel.PreviousContractCode;
            cell.arrowIcon.hidden = NO;
        }
        if (indexPath.row == 1) {
            cell.contentLabel.text = self.addOrderModel.PreviousOrderInvestmentAmount;
        }
        if (indexPath.row == 2) {
            cell.contentLabel.text = self.addOrderModel.CalulationDate;
        }
    }
    return cell;

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 43;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    WS(weakSelf)
    switch (indexPath.row) {
        case 0:
            {
                LKContinuInvestOrderListViewController *vc = [[LKContinuInvestOrderListViewController alloc] init];
                vc.isAddContinueOrder = YES;
                vc.contractCodeBlock = ^(LKOrderModel *orderModel) {
                    LKWriteContinuInvestCell *cell = [weakSelf.tableview cellForRowAtIndexPath:indexPath];
                    cell.contentLabel.text = orderModel.contractCode;
                    weakSelf.addOrderModel.PreviousContractCode = orderModel.contractCode;
                    if (orderModel.customerName) {
                        weakSelf.customerModel.customerCode = orderModel.customerCode;
                        weakSelf.addOrderModel.PreviousOrderInvestmentAmount = orderModel.investmentAmount;
                        weakSelf.addOrderModel.CalulationDate = orderModel.calulationDateTest;
                        weakSelf.addOrderModel.PreviousContractCode = orderModel.contractCode;
                        weakSelf.addOrderModel.PreviousOrderCode = orderModel.orderCode;
                        NSDate *date = [NSString getPriousorLaterDateFromDate:[NSString getDateAccordingTimeStr:self.addOrderModel.CalulationDate] withMonth:[orderModel.productTerm integerValue]];
                        NSTimeInterval interTime = 24 *60 *60;
                        NSDate *lastDate = [date dateByAddingTimeInterval:-interTime];
                        weakSelf.addOrderModel.EndEntityDate = [NSString getTimeStrAccoringDate:lastDate];
//                        weakSelf.addOrderModel.EndEntityDate = [NSString getTimeStrAccoringDate:date];
                        
                    }else{
                        weakSelf.isHandInput = YES;
                        
                    }
                    
                    [weakSelf.tableview reloadData];
                };
                vc.addOrderModel = weakSelf.addOrderModel;
                [self.navigationController pushViewController:vc animated:YES];
            }
            break;
        case 1:
        {
            
        }
            break;
        case 2:
        {
            if (self.isHandInput) {
                [GFDatePicker showCustomDatePickerAtView:[UIApplication sharedApplication].keyWindow choosedDateBlock:^(NSDate *date) {
                    NSString *dateStr = [NSString stringWithFormat:@"%@",date];
                    NSString *timeStr = [dateStr componentsSeparatedByString:@" "][0];
                    weakSelf.addOrderModel.CalulationDate = timeStr;
                    NSLog(@"选中的日期的值%@",weakSelf.addOrderModel.CalulationDate);
                    LKWriteContinuInvestCell *cell = (LKWriteContinuInvestCell *)[tableView cellForRowAtIndexPath:indexPath];
                    cell.contentLabel.text = timeStr;
                    
                } cancelBlock:^{
                    
                }];
            }else{

            }

        }
            break;
        default:
            break;
    }
}
-(NSArray *)titlesArray{
    if (_titlesArray == nil) {
        _titlesArray = @[@"选择被续投单",@"原合同金额(元)",@"原合同起息日期"];
    }
    return _titlesArray;
}

/**
 根据客户的id返回续投订单的客户的详情信息
 */
-(void)accordingOrderBackCustomerDetailInforWithCustomerId:(NSString *)customId{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
